package work1.jdbcbooksystem;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class BookDaoDB implements BookDao {
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    Connection conn =JDBCUtils.getConnection();

    @Override
    public int insert(Book book) {
            Book b = query(book.getName());
            if (b==null){
                String sql="insert into book(name,press,author,price) values(?,?,?,?)";
                try {
                    pstmt = conn.prepareStatement(sql);
                    pstmt.setString(1,book.getName());
                    pstmt.setString(2,book.getPress());
                    pstmt.setString(3,book.getAuthor());
                    pstmt.setDouble(4,book.getPrice());
                    int c=pstmt.executeUpdate();
                    return c;
                } catch (SQLException e) {
                    e.printStackTrace();
                }

            }
        return 0;
    }

    @Override
    public Book query(String str) {
        String sql ="select * from book where name=?";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,str);
            rs=pstmt.executeQuery();
            if (rs.next()){
                int id=rs.getInt("id");
                String bookName = rs.getString("name");
                String press=rs.getString("press");
                String author=rs.getString("author");
                double price=rs.getDouble("price");
                Book book = new Book(id,bookName,press,author,price);
                return book;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void select() {
        String sql="select * from book";
        try {
            pstmt = conn.prepareStatement(sql);
            rs= pstmt.executeQuery();
            while (rs.next()){
                int id =rs.getInt("id");
                String name = rs.getString("name");
                String press = rs.getString("press");
                String author = rs.getString("author");
                double price = rs.getDouble("price");
                System.out.println(id+"\t"+name+"\t"+press+"\t"+author+"\t"+price);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public ArrayList<Book> selectAll() {
        ArrayList<Book> arrayList = new ArrayList<Book>();
        String sql="select * from book";
        try {
            pstmt= conn.prepareStatement(sql);
            rs=pstmt.executeQuery();
            while (rs.next()){
                int id =rs.getInt("id");
                String name = rs.getString("name");
                String press = rs.getString("press");
                String author = rs.getString("author");
                double price = rs.getDouble("price");
                Book book=new Book(id,name,press,author,price);
                arrayList.add(book);
            }
            return arrayList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int update(Book b) {
        return 0;
    }



    @Override
    public void exit() {
        System.exit(0);

    }

    @Override
    public int delete(String name) {
        String sql = "delete from book where name=?";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,name);
        } catch (SQLException e) {
            e.printStackTrace();
            return 1;
        }
        return 0;
    }
}
